package nl.rainbowsheep.reisorg.empl.site.controller;

import java.util.List;
import javax.validation.Valid;
import nl.rainbowsheep.reisorg.empl.site.model.City;
import nl.rainbowsheep.reisorg.empl.site.service.CityService;
import nl.rainbowsheep.reisorg.empl.site.service.CountryService;
import nl.rainbowsheep.reisorg.empl.site.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    CountryService countryservice;

    @Autowired
    RegionService regionservice;

    @Autowired
    CityService cityservice;

    @RequestMapping(value = {"/", "/index", ""})
    public ModelAndView getAll() {
//        List<Region> regions = regionservice.getAll();
        List<City> cities = cityservice.getAll();
        ModelAndView model = new ModelAndView("city/index");
//        model.addObject("regions", regions);
        model.addObject("cities", cities);
        return model;
    }

    @RequestMapping(value = {"destination"})
    public List<City> getAllCities() {
        List<City> cities = cityservice.getAll();
        return cities;
    }

    @RequestMapping(value = {"/getbyregionid/{id}"})
    public @ResponseBody List<City> getAllCitiesByRegionId(@PathVariable long id) {
        List<City> cities = cityservice.getByRegionId(id);
        return cities;
    }
    
    @RequestMapping(value = {"/getbycountryid/{id}"})
    public @ResponseBody List<City> getAllCitiesByCountryId(@PathVariable long id) {
        List<City> cities = cityservice.getByCountryId(id);
        return cities;
    }
    
    @RequestMapping(value = "/details/{id}", method = RequestMethod.GET)
    public ModelAndView getCity(@PathVariable long id) {
        City city = cityservice.getById(id);
        ModelAndView mav = new ModelAndView("city/details");
        mav.addObject("city", city);
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView getCityForm() {
        ModelAndView mav = new ModelAndView("city/create");
        mav.addObject("city", new City());
        return mav;
    }

    /**
     *
     * @param country
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createTrip(@ModelAttribute("city") @Valid City city, BindingResult bindingResult) {
        cityservice.insertCity(city);
        // moet nog
        return "city/create";
    }
}
